package com.myhexin.alert;

import com.myhexin.alert.spi.AlertService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

/**
 * @author Jayce
 * @version 1.0.0
 * @date 2021/10/6
 * @desc
 */
@Slf4j
public class AlertServiceProviderFactory {
    private AlertServiceProviderFactory() {}
    private static List<AlertService> alertServiceList;
    static {
        alertServiceList = new ArrayList<>();
        ServiceLoader<AlertService> serviceLoader = ServiceLoader.load(AlertService.class);
        // 用于证明加载并实例化了两次AlertService
        //ServiceLoader<AlertService> serviceLoader2 = ServiceLoader.load(AlertService.class);

        for (AlertService service : serviceLoader) {
            if (service != null) {
                alertServiceList.add(service);
                log.info("alertService内存地址: {}", service);
            }
        }
    }

    public static List<AlertService> getAlertServiceProviders() {
        return alertServiceList;
    }
}
